package org.javaforever.gatescore.reports;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.reports.verb.ListActiveOption;
import org.javaforever.gatescore.reports.verb.ListActiveSumOption;
import org.javaforever.gatescore.reports.verb.Setup;
import org.javaforever.gatescore.widgets.Layout;

public class EchartsReportLayout extends Layout{
	protected FrontDomain reportDomain;
	protected List<FrontField> xAxisFields;
	protected FrontField yName;
	protected EchartsDiagram diagram;

	@Override
	public StatementList generateLayoutStatements() throws Exception{		
		StatementList sl = diagram.generateWidgetStatements();
		sl.setSerial(this.serial);
		return sl;
	}

	@Override
	public StatementList generateLayoutScriptStatements() throws Exception{
		StatementList sl =  diagram.generateWidgetScriptStatements();
		sl.setSerial(this.serial);
		return sl;
	}

	@Override
	public boolean parse() throws Exception{
		if (this.diagram == null) {
			ListActiveOption lao = new ListActiveOption(this.reportDomain,this.xAxisFields,this.yName);
			ListActiveSumOption laso = new ListActiveSumOption(this.reportDomain,this.xAxisFields,this.yName);
			Setup setup = new Setup(this.reportDomain,lao,laso);
			
			this.diagram = new EchartsDiagram();
			this.diagram.setDomain(this.reportDomain);
			this.diagram.setListOption(lao);
			this.diagram.setListSumOption(laso);
			this.diagram.setSetup(setup);			
			
			this.diagram.setDomain(this.reportDomain);
			this.diagram.setxAxisFields(this.xAxisFields);
			this.diagram.setyName(this.yName);
		}
		return true;
	}

	public FrontDomain getReportDomain() {
		return reportDomain;
	}

	public void setReportDomain(FrontDomain reportDomain) {
		this.reportDomain = reportDomain;
	}

	public List<FrontField> getxAxisFields() {
		return xAxisFields;
	}

	public void setxAxisFields(List<FrontField> xAxisFields) {
		this.xAxisFields = xAxisFields;
	}

	public FrontField getyName() {
		return yName;
	}

	public void setyName(FrontField yName) {
		this.yName = yName;
	}

	public EchartsDiagram getDiagram() {
		return diagram;
	}

	public void setDiagram(EchartsDiagram diagram) {
		this.diagram = diagram;
	}

	@Override
	public StatementList generateStatements() throws Exception {
		List<Writeable> sList = new ArrayList<>();
		StatementList sl = this.generateLayoutStatements();
		StatementList sls = this.generateLayoutScriptStatements();
		sl.setIndent(2);
		sl.setSerial(2000L);
		sls.setSerial(5000L);
		
		sList.add(new Statement(1000L,0,"<template>"));
		sList.add(new Statement(1500L,1,"<div>"));
		sList.add(sl);	
		sList.add(new Statement(2500L,1,"</div>"));
		sList.add(new Statement(3000L,0,"</template>"));
		sList.add(new Statement(4000L,0,"<script>"));
		sList.add(sls);
		sList.add(new Statement(6000L,0,"</script>"));		
		return WriteableUtil.merge(sList);
	}

	
}
